__author__ = 'tyrion'

from BaseStatus import BaseStatus

import  json
from src.common import GetHttpRequest
from gloal_conf import MONITOR_IP

class ServerStatus(BaseStatus):

    def __init__(self, config):
        self.svrName = config.get('svrName', "error: not found svr name ")

    def GetStatus(self):
        print "info: start to get server status"
        #http://221.228.81.136:81/check_mk/view.py?view.py?_do_confirm=yes&_transid=-1&output_format=json&_do_actions=yes&view_name=hoststatus&_username=icinga_nd&_secret=1234567890&host=nds_store_10_33_6_130
        url = "http://%s/check_mk/view.py?view.py?_do_confirm=yes&_transid=-1&output_format=json&_do_actions=yes&view_name=hoststatus&_username=icinga_nd&_secret=1234567890&host=%s" %(MONITOR_IP, self.svrName)
        print "info: url = %s" % url

        ret = GetHttpRequest(url)
        print ("info : http request ret: %s" % (ret))
        jsonRet = json.loads(ret)
        print( jsonRet)
        for index, item in enumerate(jsonRet[0]):
            if item == "host_state":
                try:
                    statusRet = jsonRet[1][index]
                    result = {self.svrName: statusRet}
                    break
                except IndexError:
                    result = "error, please check is the server exit?"

        return  result